﻿/*
 * firhttp开源项目官方网址http://node.firadio.com
 * 特点：简单、快速、长连接、无压缩、无缓存、只处理动态页面、不放图片/JS/CSS
 * 如果修改代码请保留注释，并通过SVN提交上来.
 * SVN地址https://firadionode.googlecode.com/svn
 * SVN帐号密码获取请联系作者QQ:550220138获取
 * 如果您对firhttp的研究感兴趣，可以加入我们的QQ群26499890
*/
var firhttp_bindhost='0.0.0.0';
var firhttp_bindport=3000;
var loglevel=3;
//========配置文件到这里结束=========
process.title='firhttp [ '+firhttp_bindhost+':'+firhttp_bindport+' ]';
var stdin=process.openStdin();
stdin.setEncoding('binary');
stdin.on('data',function(chunk){
	var chunk_len=chunk.length-2;
	chunk=firadio.gbk_to_utf8(chunk.substr(0,chunk_len));
	if(chunk=='exit'){
		process.exit();
	}
	process.stdout.write('data:['+chunk+']('+chunk_len+")\r\n");
});
stdin.on('end',function(){
	process.stdout.write('end');
});
//========firhttp代码开始=========
var firadio=require('firadio');
var http=firadio.http;
http.set('views',__dirname+'/html');
var net=require('net');
//var ejs=require('ejs');
var routes=require('./routes');
var model=require('./model');
var httpserver=net.createServer(function(socket){
	var remoteAddress=socket.remoteAddress;
	var remotePort=socket.remotePort;
	socket.setTimeout(20000);//20秒内无操作就自动断开
	socket.on('connect',function(){
		if(loglevel>=2)firadio.log('进入['+remoteAddress+':'+remotePort+']');
	});
	socket.on('data',function(buf){
		var $_HEADER=http.getheader(buf);
		if(loglevel>=3)firadio.log('数据['+remoteAddress+':'+remotePort+'] '+decodeURI($_HEADER['uri']));
		//读取header,cookie,get,post
		var $_COOKIE=http.get_cookie_info($_HEADER['Cookie']);
		var uri_info=http.get_uri_info($_HEADER['uri']);
		var $_GET=uri_info['get'];
		var $_POST=http.get_str_to_arr(buf.toString('utf8',$_HEADER['buf_headeroveri'],$_HEADER['buf_length']));
		var req={};
		req['header']=$_HEADER;
		req['cookie']=$_COOKIE;
		req['get']=$_GET;
		req['post']=$_POST;
		//下面开始页面输出
		//var html=firadio.date('Y-m-d H:i:s.K');
		var html='';
		switch($_GET['r']){
		case('user'):html=routes.user(req,http,model);break;
		default:html=routes.index(req,http,model);break;
		}
		var html_length=new Buffer(html).length;
		var data="HTTP/1.1 200 OK\r\n";
		data+="Content-Type: text/html; charset=utf-8\r\n";
		data+="Pragma: no-cache\r\n";
		data+="Cache-Control: max-age=0\r\n";
		data+="Connection: Keep-Alive\r\n";
		data+="Content-Length: "+html_length+"\r\n";
		data+="\r\n"+html;
		socket.write(data);
	});
	socket.on('end',function(){
		if(loglevel>=4)firadio.log('离开['+remoteAddress+':'+remotePort+']');
		socket.end();//关闭客户端的连接
	});
	socket.on('timeout',function(){
		if(loglevel>=4)firadio.log('超时['+remoteAddress+':'+remotePort+']');
		socket.end();//关闭客户端的连接
	});
	socket.on('drain',function(){
		if(loglevel>=4)firadio.log('成功['+remoteAddress+':'+remotePort+']');
	});
	socket.on('error',function(){
		if(loglevel>=2)firadio.log('出错['+remoteAddress+':'+remotePort+']');
		socket.end();//关闭客户端的连接
	});
	socket.on('close',function(){
		if(loglevel>=2)firadio.log('断开['+remoteAddress+':'+remotePort+']');
		socket.end();//关闭客户端的连接
	});
});
httpserver.on('error',function(data){
	var errorinfo=data.toString();
	if(errorinfo=='Error: listen EADDRINUSE'){
		errorinfo='监听端口被占用！';
	}
	firadio.log('firhttp：'+errorinfo);
	process.exit();
});
httpserver.listen(firhttp_bindport,firhttp_bindhost,function(){
	var address=httpserver.address();
	firadio.log('firhttp服务成功启动在'+address['address']+':'+address['port']);
});
